package iotwisp.school.api.controller.pc_admin.user;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.JSONUtils;
import iotwisp.school.user.entity.AdminMenu;
import iotwisp.school.user.entity.Permission;
import iotwisp.school.user.qo.AdminMenuQO;
import iotwisp.school.user.service.AdminMenuService;

@RestController
@RequestMapping("/api/org/admin_menu")
public class AdminMenuController extends RestBaseController {

	@Autowired
	private AdminMenuService adminMenuService;

	@RequestMapping(value = "/query", method = { RequestMethod.POST, RequestMethod.GET })
	public ResponseEntity<JSONResultDTO> queryAdminMenu(@RequestBody(required = false) AdminMenuQO qoBody,
			@RequestParam(name = "qo", required = false) String qoJson) throws UnsupportedEncodingException {

		AdminMenuQO qo;
		if (!StringUtils.isBlank(qoJson)) {
			qo = JSONUtils.parseObject(URLDecoder.decode(qoJson, "UTF-8"), AdminMenuQO.class);
		} else {
			qo = qoBody;
		}

		switch (qo.getResultType()) {
		case AdminMenuQO.RESULT_TYPE_COUNT:
			Integer count = adminMenuService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case AdminMenuQO.RESULT_TYPE_UNIQUE:
			AdminMenu adminMenu = adminMenuService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(adminMenu));
		case AdminMenuQO.RESULT_TYPE_LIST:
			List<AdminMenu> adminMenus = adminMenuService.queryList(qo);
			if (qo.getReturnTree()) {
				adminMenus = transToTree(adminMenus);
			}
			return ResponseEntity.ok(createSuccessResult(adminMenus));
		case AdminMenuQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = adminMenuService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}

	}

	private List<AdminMenu> transToTree(List<AdminMenu> adminMenus) {

		List<AdminMenu> list = new ArrayList<>();
		Map<String, List<Permission>> adminMenuChildren = new HashMap<>();

		for (AdminMenu adminMenu : adminMenus) {
			if (!StringUtils.isBlank(adminMenu.getTreeData().getParentId())) {
				if (!adminMenuChildren.containsKey(adminMenu.getTreeData().getParentId())) {
					adminMenuChildren.put(adminMenu.getTreeData().getParentId(), new ArrayList<>());
				}
				adminMenuChildren.get(adminMenu.getTreeData().getParentId()).add(adminMenu);
			}
		}

		for (AdminMenu adminMenu : adminMenus) {
			if (StringUtils.isBlank(adminMenu.getTreeData().getParentId())) {
				adminMenu.setChildren(adminMenuChildren.get(adminMenu.getId()));
				list.add(adminMenu);
			}
		}
		return list;
	}

}
